/*
 * $Id: Logo.java 56 2010-01-23 14:49:04Z aaron.day $
 */
package org.crazydays.gameplan;


import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;

/**
 * Image
 */
public class Logo
{
    /**
     * Main.
     * 
     * @param arguments Arguments
     */
    public static void main(String[] arguments)
    {
        int height = 64;
        int width = 64;

        Logo logo = new Logo(width, height);

        try {
            logo.write(new File("logo.png"));
        } catch (IOException e) {
            System.out.println(e.toString());
        }
    }

    /** image */
    protected BufferedImage image;

    /**
     * Logo constructor.
     * 
     * @param width Width
     * @param height Height
     */
    public Logo(int width, int height)
    {
        initialize(width, height);
        render(width, height);
    }

    /**
     * Initialize logo.
     * 
     * @param height Height
     * @param width Width
     */
    protected void initialize(int width, int height)
    {
        image = new BufferedImage(height, width, BufferedImage.TYPE_4BYTE_ABGR);

    }

    /**
     * Render.
     * 
     * @param width Width
     * @param height Height
     */
    protected void render(int width, int height)
    {
        renderSquare(width, height);
        renderIsometric(width, height);
        renderHexagon(width, height);
    }

    /**
     * Render square.
     * 
     * @param width Width
     * @param height Height
     */
    protected void renderSquare(int width, int height)
    {
        Graphics2D graphics = (Graphics2D) image.getGraphics();

        BasicStroke stroke =
            new BasicStroke((height + width) * .012f, BasicStroke.CAP_ROUND,
                BasicStroke.JOIN_ROUND, (height + width) * .012f);

        graphics.setStroke(stroke);

        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);

        graphics.setColor(new Color(0, 0, 255, 0x80));

        int count = 4;
        int xs[] =
            { (int) (width * .10), (int) (width * .10), (int) (width * .60),
                (int) (width * .60) };
        int ys[] =
            { (int) (height * .80), (int) (height * .30), (int) (height * .30),
                (int) (height * .80) };

        graphics.drawPolygon(xs, ys, count);
    }

    /**
     * Render isometric.
     * 
     * @param width Width
     * @param height Height
     */
    protected void renderIsometric(int width, int height)
    {
        Graphics2D graphics = (Graphics2D) image.getGraphics();

        BasicStroke stroke =
            new BasicStroke((height + width) * .012f, BasicStroke.CAP_ROUND,
                BasicStroke.JOIN_ROUND, (height + width) * .012f);

        graphics.setStroke(stroke);

        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);

        graphics.setColor(new Color(0, 255, 0, 0x80));

        int count = 4;
        int xs[] =
            { (int) (width * .55), (int) (width * .95), (int) (width * .55),
                (int) (width * .15) };
        int ys[] =
            { (int) (height * .45), (int) (height * .25), (int) (height * .05),
                (int) (height * .25) };

        graphics.drawPolygon(xs, ys, count);
    }

    /**
     * Render hexagon.
     * 
     * @param width Width
     * @param height Height
     */
    protected void renderHexagon(int width, int height)
    {
        Graphics2D graphics = (Graphics2D) image.getGraphics();

        BasicStroke stroke =
            new BasicStroke((height + width) * .012f, BasicStroke.CAP_ROUND,
                BasicStroke.JOIN_ROUND, (height + width) * .012f);

        graphics.setStroke(stroke);

        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);

        graphics.setColor(new Color(255, 0, 0, 0x80));

        int count = 6;
        int xs[] =
            { (int) (width * .50), (int) (width * .80), (int) (width * .95),
                (int) (width * .80), (int) (width * .50), (int) (width * .35) };
        int ys[] =
            { (int) (height * .40), (int) (height * .40), (int) (height * .65),
                (int) (height * .90), (int) (height * .90),
                (int) (height * .65) };

        graphics.drawPolygon(xs, ys, count);
    }

    /**
     * Get image.
     * 
     * @return Image
     */
    public BufferedImage getImage()
    {
        return image;
    }

    /**
     * Write logo to file.
     * 
     * @param file File
     * @throws IOException
     */
    public void write(File file)
        throws IOException
    {
        OutputStream outputStream = new FileOutputStream(file);

        try {
            ImageIO.write(image, "png", outputStream);
        } finally {
            outputStream.close();
        }
    }
}
